<template>
  <div class="mod-config">
    <basic-container>
      <div class="boxsty zhiBiao">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <div class="flex justifyContentSpaceBetween"></div>
          <el-form
            :inline="true"
            :model="dataForm"
            @keyup.enter.native="getDataList()"
            class="form-inline"
          >
            <el-form-item
              label="指标类型"
              v-if="normLxs.length>0&&(dataForm.normType=='htrw_zb'||dataForm.normType=='rckh_zb')"
              prop="normLx"
            >
              <el-select
                :clearable="true"
                v-model="dataForm.normLx"
                placeholder="请选择指标类型"
                style="margin-left:10px;width:160px"
              >
                <el-option
                  v-for="item in normLxs "
                  :key="item.value"
                  :label="item.label"
                  :value="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="指标名称">
              <el-input v-model="dataForm.normName" placeholder="请输入指标名称" style="width:160px"></el-input>
            </el-form-item>
            <el-form-item label="数据状态">
              <el-select
                :clearable="true"
                v-model="dataForm.dataState"
                placeholder="请选择状态"
                style="margin-left:10px;width:160px"
                class="xiaLa"
              >
                <el-option
                  v-for="item in [{value:0,label:'删除' },{value:1,label:'正常'}]"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
              <el-button
                icon="el-icon-search"
                v-if="permissions.sysnorm_sysnorm_view"
                type="primary"
                class="marginLeft"
                @click="getDataList()"
              >检索</el-button>
            </el-form-item>
          </el-form>
          <div style="float: right;margin-top: -70px;">
            <el-link
              icon="el-icon-plus"
              v-if="permissions.sysnorm_sysnorm_add"
              type="primary"
              @click="addOrUpdateHandle()"
              :underline="false"
            >新增指标信息</el-link>
          </div>
          <el-tab-pane
            :label="it.label"
            :name="'tab_' +index"
            v-for="(it,index) in zbfl"
            :key="it.value"
          >
            <div class="avue-crud">
              <el-table :data="dataList" v-loading="dataListLoading" stripe tooltip-effect="light">
                <el-table-column prop label="序号" :formatter="nShowIndex" width="80"></el-table-column>
                <!-- <el-table-column prop="normType" label="指标库类型" :formatter="formaterType" min-width="150">
                </el-table-column>-->
                <el-table-column
                  prop="normLx"
                  v-if="dataForm.normType=='htrw_zb'||dataForm.normType=='rckh_zb'"
                  label="指标类型"
                  min-width="150"
                ></el-table-column>
                <el-table-column
                  prop="normFl"
                  v-if="dataForm.normType=='htrw_zb'||dataForm.normType=='rckh_zb'"
                  label="指标分类"
                  min-width="150"
                >
                  <template slot-scope="scope">
                    <span v-if="scope.row.normFl =='gx' ">个性指标</span>
                    <span v-if="scope.row.normFl =='ty' ">通用指标</span>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="normGsname"
                  v-if="dataForm.normType=='htrw_zb'||dataForm.normType=='rckh_zb'"
                  label="计算公式"
                  min-width="150"
                  show-overflow-tooltip
                ></el-table-column>
                <el-table-column prop="normName" label="指标名称" min-width="150" show-overflow-tooltip></el-table-column>
                <el-table-column
                  prop="normDw"
                  v-if="dataForm.normType=='htrw_zb'||dataForm.normType=='rckh_zb'"
                  label="指标单位"
                  min-width="150"
                ></el-table-column>
                <!-- <el-table-column prop="normValue" label="指标值" min-width="100">
                </el-table-column>-->
                <el-table-column prop="normSm" show-overflow-tooltip label="指标说明" min-width="150"></el-table-column>
                <el-table-column prop="normRule" show-overflow-tooltip min-width="120" label="指标规则"></el-table-column>
                <el-table-column prop="normGetDate" show-overflow-tooltip min-width="150" label="数据获取方式"></el-table-column>
                <el-table-column prop="normPx" label="指标序号" min-width="100"></el-table-column>
                <el-table-column label="操作" width="220">
                  <template slot-scope="scope">
                    <el-button type="primary" plain @click="copyHandle(scope.row.normId)">复制</el-button>
                    <el-button
                      v-if="permissions.sysnorm_sysnorm_edit"
                      type="primary"
                      plain
                      @click="addOrUpdateHandle(scope.row.normId)"
                    >修改</el-button>
                    <el-button
                      v-if="permissions.sysnorm_sysnorm_del"
                      type="danger"
                      plain
                      @click="deleteHandle(scope.row.normId)"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div class="avue-crud__pagination">
              <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                background
                layout="total, sizes, prev, pager, next, jumper"
              ></el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, delObj, copyHandle } from "@/api/admin/sysnorm";
import { remote as remoteDict } from "@/api/admin/dict";
import TableForm from "./sysnorm-form";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      normTypeIndex: 0,
      zbmc: "",
      activeName: "tab_0",
      zbfl: [
        {
          label: "合同任务指标库",
          value: "htrw_zb"
        },
        {
          label: "精准激励指标库",
          value: "rckh_zb"
        },
        {
          label: "测评项指标库",
          value: "cp-pfx"
        },
        {
          label: "评价等次指标库",
          value: "cp-pjdc"
        },
        {
          label: "评分规则指标库",
          value: "cp-pfgz"
        }
      ],
      normLxs: [],
      dataForm: {
        normName: "",
        dataState: 1,
        normType: "",
        normLx: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    };
  },
  components: {
    TableForm
  },
  mounted() {
    remoteDict("sys_norm_tyupe_subtype").then(response => {
      this.normLxs = response.data.data;
    });
    this.dataForm.normType = this.zbfl[0].value;
    this.zbmc = this.zbfl[0].label;
    this.getDataList();
  },
  computed: {
    ...mapGetters(["permissions"])
  },

  methods: {
    handleClick(tab, event) {
      this.normTypeIndex = tab.index;
      this.zbmc = tab.label;
      this.dataForm.normName = "";
      this.dataForm.normType = this.zbfl[tab.index].value;
      this.dataList = [];
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      fetchList(
        Object.assign(
          {
            current: this.pageIndex,
            size: this.pageSize
          },
          this.dataForm
        )
      ).then(response => {
        this.dataList = response.data.data.records;
        this.totalPage = response.data.data.total;
      });
      this.dataListLoading = false;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    copyHandle(id) {
      this.$confirm("是否复制这条指标？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return copyHandle(id);
        })
        .then(data => {
          this.$message.success("复制成功");
          this.getDataList();
        });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, this.dataForm.normType, this.zbfl);
      });
    },
    // 删除
    deleteHandle(id) {
      this.$confirm("是否确认删除ID为" + id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(id);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getDataList();
        });
    },
    formaterType(row) {
      if (row.normType) {
        if (row.normType == "cp-pfx") {
          return "测评-评分项";
        } else if (row.normType == "cp-pjdc") {
          return "测评-评价等次";
        } else if (row.normType == "cp-pfgz") {
          return "测评-评分规则";
        } else {
          return "";
        }
      } else {
        return "";
      }
    },
    nShowIndex(row, column, cellValue, index) {
      return index + 1;
    }
  }
};
</script>
